package com.mianyang.zhangsan.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mianyang.zhangsan.common.base.BaseController;
import com.mianyang.zhangsan.common.base.R;
import com.mianyang.zhangsan.model.dto.SecProblemAnswerDto;
import com.mianyang.zhangsan.model.entity.SecProblem;
import com.mianyang.zhangsan.service.SecBankService;
import com.mianyang.zhangsan.service.SecProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/problem")
public class SecProblemController extends BaseController {
    @Autowired
    private SecProblemService problemService;
    @Autowired
    private SecBankService bankService;


    /**
     * 查询试题
     */
    @GetMapping("/show")
    public R showBank(SecProblem problem,
                      @RequestParam(value = "current", defaultValue = "1") Integer current,
                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SecProblem> problems = problemService.selectProOfBank(problem, current, pageSize);
        return success("problems", problems);
    }

    /**
     * 修改试题
     */
    @PutMapping("/edit")
    public R editProblem(@RequestBody SecProblemAnswerDto problemInfo) {
        problemService.updateProblemAndAnswer(problemInfo);
        return success();
    }

    /**
     * 新增试题
     */
    @PostMapping("/save")
    public R saveProblem(@RequestBody SecProblemAnswerDto problemInfo) {
        bankService.saveProblemAndAnswer(problemInfo);
        return success();
    }

    /**
     * 删除试题
     */
    @DeleteMapping("/delete/{problemId}")
    public R deleteProblem(@PathVariable("problemId") Long problemId) {
        problemService.deleteProblemAndAnswer(problemId);
        return success();
    }
}
